﻿@page "/resources"
@using  Csla.Blazor
@using  Csla.Rules
@inject ViewModel<ProjectTracker.Library.ResourceList> vm

<h1>Resources</h1>

<p class="text-danger">@vm.ViewModelErrorText</p>

@if (vm.Model == null)
{
  <p>Loading...</p>
}
else
{
  <AuthorizeView Policy="@(CslaPolicy.GetPolicy(AuthorizationActions.CreateObject, typeof(ProjectTracker.Library.ResourceEdit)))">
    <p><a href="resourceedit">Add resource</a></p>
  </AuthorizeView>
  <p>

  </p>
  <table class="table">
    <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      @foreach (var item in vm.Model)
      {
        <tr>
          <td>@item.Id</td>
          <td>@item.Name</td>
          <td>
            @if (item.Id == itemSelectedForDeletion)
            {
              <span class="alert-danger"><a href="javascript: void(0);" @onclick="() => Delete(item.Id)">Confirm delete</a></span>
              <span> | </span><a href="javascript: void(0);" @onclick="() => itemSelectedForDeletion = -1">Cancel</a>
            }
            else
            {
              <AuthorizeView Policy="@(CslaPolicy.GetPolicy(AuthorizationActions.EditObject, typeof(ProjectTracker.Library.ResourceEdit)))">
                <a href="resourceedit/@item.Id">Edit</a>
                <span> | </span>
              </AuthorizeView>
              <AuthorizeView Policy="@(CslaPolicy.GetPolicy(AuthorizationActions.DeleteObject, typeof(ProjectTracker.Library.ResourceEdit)))">
                <a href="javascript: void(0);" @onclick="() => SelectForDelete(item.Id)">Delete</a>
              </AuthorizeView>
            }
          </td>
        </tr>
      }
    </tbody>
  </table>
}

@code {
  private int itemSelectedForDeletion = -1;

  protected override async Task OnParametersSetAsync()
  {
    await vm.RefreshAsync(() => Csla.DataPortal.FetchAsync<ProjectTracker.Library.ResourceList>());
  }

  private void SelectForDelete(int id)
  {
    itemSelectedForDeletion = id;
  }

  private async void Delete(int id)
  {
    itemSelectedForDeletion = -1;
    vm.Model = null;
    await ProjectTracker.Library.ResourceEdit.DeleteResourceEditAsync(id);
    await vm.RefreshAsync(() => Csla.DataPortal.FetchAsync<ProjectTracker.Library.ResourceList>());
    StateHasChanged();
  }
}
